package cn.iocoder.yudao.module.hs.service.qcorderdetail;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.hs.controller.admin.qcorderdetail.vo.QcOrderDetailPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.qcorderdetail.vo.QcOrderDetailSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.qcorderdetail.QcOrderDetailDO;
import cn.iocoder.yudao.module.hs.dal.mysql.qcorderdetail.QcOrderDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.QC_ORDER_DETAIL_NOT_EXISTS;

/**
 * 质检单明细 Service 实现类
 *
 * @author 航盛
 */
@Service
@Validated
public class QcOrderDetailServiceImpl implements QcOrderDetailService {

    @Resource
    private QcOrderDetailMapper qcOrderDetailMapper;

    @Override
    public Long createQcOrderDetail(QcOrderDetailSaveReqVO createReqVO) {
        // 插入
        QcOrderDetailDO qcOrderDetail = BeanUtils.toBean(createReqVO, QcOrderDetailDO.class);
        qcOrderDetailMapper.insert(qcOrderDetail);
        // 返回
        return qcOrderDetail.getId();
    }

    @Override
    public void updateQcOrderDetail(QcOrderDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateQcOrderDetailExists(updateReqVO.getId());
        // 更新
        QcOrderDetailDO updateObj = BeanUtils.toBean(updateReqVO, QcOrderDetailDO.class);
        qcOrderDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteQcOrderDetail(Long id) {
        // 校验存在
        validateQcOrderDetailExists(id);
        // 删除
        qcOrderDetailMapper.deleteById(id);
    }

    private void validateQcOrderDetailExists(Long id) {
        if (qcOrderDetailMapper.selectById(id) == null) {
            throw exception(QC_ORDER_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public QcOrderDetailDO getQcOrderDetail(Long id) {
        return qcOrderDetailMapper.selectById(id);
    }

    @Override
    public PageResult<QcOrderDetailDO> getQcOrderDetailPage(QcOrderDetailPageReqVO pageReqVO) {
        return qcOrderDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public QcOrderDetailDO getQcOrderDetail(String materialCode) {
        LambdaQueryWrapperX<QcOrderDetailDO> queryWrapper = new LambdaQueryWrapperX<QcOrderDetailDO>()
                .eq(QcOrderDetailDO::getMaterialCode, materialCode)
                .orderByDesc(QcOrderDetailDO::getCreateTime);
        List<QcOrderDetailDO> qcOrderDetailDOS = qcOrderDetailMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(qcOrderDetailDOS)) {
            return qcOrderDetailDOS.get(0);
        }
        return null;
    }

    @Override
    public List<QcOrderDetailDO> getQcOrderDetailList(Long qcOrderId) {
        return qcOrderDetailMapper.selectList(QcOrderDetailDO::getQcOrderId, qcOrderId);
    }

    @Override
    public List<QcOrderDetailDO> getQcOrderDetailList(Set<String> materialCodeSet, List<String> qcResults) {
        LambdaQueryWrapperX<QcOrderDetailDO> queryWrapper = new LambdaQueryWrapperX<QcOrderDetailDO>()
                .in(QcOrderDetailDO::getMaterialCode, materialCodeSet)
                .inIfPresent(QcOrderDetailDO::getQcResult, qcResults);
        return qcOrderDetailMapper.selectList(queryWrapper);
    }

}